package com.konne.nightmare.FastPublicOpinion.utils;

import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.widget.ImageView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.konne.nightmare.FastPublicOpinion.base.MyApplication;


import jp.wasabeef.glide.transformations.GrayscaleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;


public class GlideUtils {
    /**
     * Load image.
     *
     * @param url       the url
     * @param imageView the image view
     */
//https://blog.csdn.net/zhangyiminsunshine/article/details/78051435
    /*
     *加载图片(默认)
     */
    public static void loadImage(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                //    .placeholder(R.mipmap.ic_zhanwei_banner) //占位图
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(MyApplication.getContext()).load(url).apply(options).into(imageView);
    }


//    public static void loadImageNew(String url, ImageView imageView) {
//        RequestOptions options = new RequestOptions()
//                .fitCenter()
//                .dontAnimate()
//                //    .placeholder(R.mipmap.ic_zhanwei_banner) //占位图
//                // .priority(Priority.HIGH)
//                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
//        Glide.with(MyApplication.getContext()).load(url).apply(options).fitCenter().skipMemoryCache(true).listener(new RequestListener<Drawable>() {
//            @Override
//            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                return false;
//            }
//
//            @Override
//            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                /*
//                 * 在此处填写具体载入逻辑*/
//
//                /*
//                判断imageView对象是否为空
//                 */
//                if (imageView == null) {
//                    return false;
//                }
//                /*
//                判断imageView的填充方式,如果不是fitxy的填充方式 设置其填充方式
//                 */
//                if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
//                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//                }
//                /*
//                进行宽度为matchparent时的适应imageView的高度计算
//                 */
//                ViewGroup.LayoutParams params = imageView.getLayoutParams();
//                int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
//                float scale = (float) vw / (float) resource.getIntrinsicWidth();
//                int vh = Math.round(resource.getIntrinsicHeight() * scale);
//                params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
//                imageView.setLayoutParams(params);
//                return false;
//            }
//        }).into(imageView);
//    }


    public class TransformationUtils extends ImageViewTarget<Bitmap> {
        private ImageView target;

        public TransformationUtils(ImageView target) {
            super(target);
            this.target = target;
        }

        @Override
        protected void setResource(Bitmap resource) {
            view.setImageBitmap(resource);        //获取原图的宽高
            int width = resource.getWidth();
            int height = resource.getHeight();        //获取imageView的宽
            int imageViewWidth = target.getWidth();        //计算缩放比例
            float sy = (float) (imageViewWidth * 0.1) / (float) (width * 0.1);
            //计算图片等比例放大后的高
            int imageViewHeight = (int) (height * sy);
            ViewGroup.LayoutParams params = target.getLayoutParams();
            params.height = imageViewHeight;
            target.setLayoutParams(params);
        }
    }

    /*
     *加载图片(本地)
     */
    public static void loadlocalImage(int res, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                //    .placeholder(R.mipmap.ic_zhanwei_banner) //占位图
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(MyApplication.getContext()).load(res).apply(options).into(imageView);
    }

    /*
     *加载图片(path)
     */
    public static void loadlocalImage(String path, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                //    .placeholder(R.mipmap.ic_zhanwei_banner) //占位图
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(MyApplication.getContext()).load(path).apply(options).into(imageView);
    }

    /*
     *加载圆形图片
     */
    public static void loadCircleImage(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .circleCrop()//设置圆形
                // .placeholder(R.color.white)
                //     .error(R.mipmap.ic_zhanwei_head)
                //.priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(MyApplication.getContext()).load(url).apply(options).into(imageView);
    }

    /*
     *加载图片,不裁剪
     */
    public static void loadCircleImage1(String path, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .fitCenter()
                .dontAnimate()
                //    .placeholder(R.mipmap.ic_zhanwei_banner) //占位图
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(MyApplication.getContext()).load(path).apply(options).into(imageView);
    }
    /*
     *加载圆形图片
     */
    public static void loadCircleThisImage(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .circleCrop()//设置圆形
                // .placeholder(R.color.white)
                //     .error(R.mipmap.ic_zhanwei_head)
                //.priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(MyApplication.getContext()).load(url).apply(options).into(imageView);
    }

    /*
     *加载圆角图片
     */
    public static void loadRoundCircleImage(String url, ImageView imageView, int radius) {

        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(radius);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300)
                .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                .skipMemoryCache(true);//不做内存缓存

        Glide.with(MyApplication.getContext()).load(url).apply(options).into(imageView);


    }


    /*
     *加载灰度(黑白)图片（自定义透明度）
     */
    public static void loadBlackImage(String url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                // .placeholder(R.color.white)
                //  .error(R.color.white)
                //  .priority(Priority.HIGH)
                .bitmapTransform(new GrayscaleTransformation())
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(MyApplication.getContext()).load(url).apply(options).into(imageView);
    }

    /**
     * 加载第四秒的帧数作为封面
     * url就是视频的地址
     */
    public static void loadCover(String url, ImageView imageView) {

        RequestOptions options = new RequestOptions();
        options.skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL)
                .frame(4000)
//                .circleCrop()//设置圆形
                //    .placeholder(R.color.white)
                //   .error(R.mipmap.ic_zhanwei_banner)
                //.priority(Priority.HIGH)
//                .skipMemoryCache(FA)
                .bitmapTransform(new RoundedCornersTransformation(10, 0, RoundedCornersTransformation.CornerType.ALL));

        Glide.with(imageView)
                .setDefaultRequestOptions(options)
                .load(url)
                .into(imageView);
    }

    private GlideUtils() {
    }

//    private static GlideUtils instance;
//
//    public static GlideUtils createGlideEngine() {
//        if (null == instance) {
//            synchronized (GlideEngine.class) {
//                if (null == instance) {
//                    instance = new GlideUtils();
//                }
//            }
//        }
//        return instance;
//    }

}
